package com.jason.core.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = { "com.jason.mapper" })
public class MybatisPlusConfig {

    /*
     * 分页插件，自动识别数据库类型
     * 多租户，请参考官网【插件扩展】
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }


    /***
     * plus 的性能优化
     * @return
     */
   /* @Bean
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        *//*<!-- SQL 执行性能分析，开发环境使用，线上不推荐。 maxTime 指的是 sql 最大执行时长 -->*//*
        performanceInterceptor.setMaxTime(1000);
        *//*<!--SQL是否格式化 默认false-->*//*
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }*/

    @Bean
    @ConfigurationProperties("spring.datasource.druid" )
    public DataSource dataSource() {
        return DruidDataSourceBuilder
                .create()
                .build();
    }
    //    mybatisPlus全局配置
   /* @Bean
    public GlobalConfiguration globalConfig(
            @Value("${mybatis-plus.global-config.id-type}") Integer idType, //主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
            @Value("${mybatis-plus.global-config.field-strategy}") Integer fieldStrategy, //字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
            @Value("${mybatis-plus.global-config.db-column-underline}") Boolean dbColumnUnderline, //驼峰下划线转换
            @Value("${mybatis-plus.global-config.refresh-mapper}") Boolean isRefresh, //刷新mapper 调试神器
            @Value("${mybatis-plus.global-config.capital-mode}") Boolean isCapitalMode, //数据库大写下划线转换
            @Value("${mybatis-plus.global-config.logic-delete-value}") String logicDeleteValue, //逻辑删除配置
            @Value("${mybatis-plus.global-config.logic-not-delete-value}") String logicNotDeleteValue //逻辑删除配置
    ) {
        GlobalConfiguration globalConfig = new GlobalConfiguration();
        if ( idType != null) {
            globalConfig.setIdType(idType);  //主键类型
        }
        if ( fieldStrategy != null) {
            globalConfig.setFieldStrategy(fieldStrategy); //字段策略
        }
        if ( dbColumnUnderline) {
            globalConfig.setDbColumnUnderline(dbColumnUnderline);  //驼峰下划线转换
        }
        if ( isRefresh) {
            globalConfig.setRefresh(isRefresh); //刷新mapper 调试神器
        }
        if (isCapitalMode) {
            globalConfig.setCapitalMode(isCapitalMode); //数据库大写下划线转换
        }
        if (logicDeleteValue != null) {
            globalConfig.setLogicDeleteValue(logicDeleteValue);  //逻辑删除配置
        }
        if (logicNotDeleteValue != null) {
            globalConfig.setLogicNotDeleteValue(logicNotDeleteValue);  //逻辑删除配置
        }
        return globalConfig;
    }*/

   /* @Bean("mybatisSqlSession")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, ResourceLoader resourceLoader, GlobalConfiguration globalConfiguration) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setTypeAliasesPackage("com.jason.entity");
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        *//*sqlSessionFactory.setMapperLocations(new ClassPathResource[]{new ClassPathResource("com/jason/mapper/xml/*.xml")});
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactory.setConfiguration(configuration);*//*
//        sqlSessionFactory.setConfiguration(mybatisConfiguration);
        sqlSessionFactory.setPlugins(new Interceptor[]{
                new PaginationInterceptor(),
                new PerformanceInterceptor(),
                new OptimisticLockerInterceptor()
        });
        sqlSessionFactory.setGlobalConfig(globalConfiguration);
        return sqlSessionFactory.getObject();
    }*/


    /*@Bean
    public GlobalConfiguration globalConfiguration() {
        GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector());
        conf.setLogicDeleteValue("-1");
        conf.setLogicNotDeleteValue("1");
        conf.setIdType(2);
        return conf;
    }*/
   /*
    * oracle数据库配置JdbcTypeForNull
    * 参考：https://gitee.com/baomidou/mybatisplus-boot-starter/issues/IHS8X
    不需要这样配置了，参考 yml:
    mybatis-plus:
      confuguration
        dbc-type-for-null: 'null'
   @Bean
   public ConfigurationCustomizer configurationCustomizer(){
       return new MybatisPlusCustomizers();
   }

   class MybatisPlusCustomizers implements ConfigurationCustomizer {

       @Override
       public void customize(org.apache.ibatis.session.Configuration configuration) {
           configuration.setJdbcTypeForNull(JdbcType.NULL);
       }
   }
   */



}
